package banco;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import dominio.Cliente;
import dominio.Debitos;
import dominio.Valores;
import fachada.FachadaDebitos;

public class DebitosDAO implements FachadaDebitos {

public void insertDebitos(Debitos d, Cliente c, Valores v) throws ClassNotFoundException, IOException {
        
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        
        try {
            conn = Banco.getConexaoEXTRATOPAES();
            stmt = conn.prepareStatement("insert into debitos values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " +
            		"?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " +
            		"?, ?, ?, ?, ?, ?)");
            stmt.setFloat(1, d.getSimples_princ());
            stmt.setFloat(2, d.getSimples_jurmul());
            stmt.setFloat(3, d.getIrrf_princ());
            stmt.setFloat(4, d.getIrrf_jurmul());
            stmt.setFloat(5, d.getPis_princ());
            stmt.setFloat(6, d.getPis_jurmul());
            stmt.setFloat(7, d.getCofins_princ());
            stmt.setFloat(8, d.getCofins_jurmul());
            stmt.setFloat(9, d.getIrpj_princ());
            stmt.setFloat(10, d.getIrpj_jurmul());
            stmt.setFloat(11, d.getCsll_princ());
            stmt.setFloat(12, d.getCsll_jurmul());
            stmt.setFloat(13, d.getMultas_decl_princ());
            stmt.setFloat(14, d.getMultas_decl_jurmul());
            stmt.setFloat(15, d.getTotal_simples());
            stmt.setFloat(16, d.getTotal_irrf());
            stmt.setFloat(17, d.getTotal_pis());
            stmt.setFloat(18, d.getTotal_cofins());
            stmt.setFloat(19, d.getTotal_irpj());
            stmt.setFloat(20, d.getTotal_csll());
            stmt.setFloat(21, d.getTotal_multas());
            stmt.setFloat(22, d.getTotal_vrprinc());
            stmt.setFloat(23, d.getTotal_jurmul());
            stmt.setFloat(24, d.getTotal_total());
            stmt.setString(25, c.getCnpj().trim());
            stmt.setInt(26, v.getId());
            stmt.executeUpdate();
        }
        catch (SQLException ex) {
            ex.printStackTrace();
        }
        
        catch (Exception e) {
        	e.printStackTrace();
        }
        finally {
	        Banco.closeConexao(conn, rs, stmt);
        }
    }

@SuppressWarnings("finally")
public Debitos selectDebitos(Cliente c, Valores v) throws ClassNotFoundException, IOException {
    
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    Debitos d = new Debitos();
    
    try {
        conn = Banco.getConexaoEXTRATOPAES();
        stmt = conn.prepareStatement("select * from debitos where cnpj_cliente = ? and id_valores = ?");
        stmt.setString(1, c.getCnpj().trim());
        stmt.setInt(2, v.getId());
        rs = stmt.executeQuery();
        while(rs.next()) {
	        d.setSimples_princ(rs.getFloat("simples_princ"));
	        d.setSimples_jurmul(rs.getFloat("simples_jurmul"));
	        d.setIrrf_princ(rs.getFloat("irrf_princ"));
	        d.setIrrf_jurmul(rs.getFloat("irrf_jurmul"));
	        d.setPis_princ(rs.getFloat("pis_princ"));
	        d.setPis_jurmul(rs.getFloat("pis_jurmul"));
	        d.setCofins_princ(rs.getFloat("cofins_princ"));
	        d.setCofins_jurmul(rs.getFloat("cofins_jurmul"));
	        d.setIrpj_princ(rs.getFloat("irpj_princ"));
	        d.setIrpj_jurmul(rs.getFloat("irpj_jurmul"));
	        d.setCsll_princ(rs.getFloat("csll_princ"));
	        d.setCsll_jurmul(rs.getFloat("csll_jurmul"));
	        d.setMultas_decl_princ(rs.getFloat("multas_princ"));
	        d.setMultas_decl_jurmul(rs.getFloat("multas_jurmul"));
	        d.setTotal_simples(rs.getFloat("total_simples"));
	        d.setTotal_irrf(rs.getFloat("total_irrf"));
	        d.setTotal_pis(rs.getFloat("total_pis"));
	        d.setTotal_cofins(rs.getFloat("total_cofins"));
	        d.setTotal_irpj(rs.getFloat("total_irpj"));
	        d.setTotal_csll(rs.getFloat("total_csll"));
	        d.setTotal_multas(rs.getFloat("total_multas"));
	        d.setTotal_vrprinc(rs.getFloat("total_vrprinc"));
	        d.setTotal_jurmul(rs.getFloat("total_jurmul"));
	        d.setTotal_total(rs.getFloat("total_total"));
        }
    }
    catch (SQLException ex) {
        ex.printStackTrace();
    }
    finally {
        Banco.closeConexao(conn, rs, stmt);
        return d;
    }
}

public void updateDebitos(Debitos d, Valores v) throws ClassNotFoundException, IOException {
    
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    
    System.out.println("ID VALORES NO DEBITO: " + v.getId());
    
    try {
        conn = Banco.getConexaoEXTRATOPAES();
        stmt = conn.prepareStatement("update debitos set simples_princ = ?," +
        		" simples_jurmul = ?, irrf_princ = ?, irrf_jurmul = ?, pis_princ = ?, pis_jurmul = ?, cofins_princ = ?, " +
        		"cofins_jurmul = ?, irpj_princ = ?, irpj_jurmul = ?, csll_princ = ?, csll_jurmul = ?, multas_princ = ?," +
        		" multas_jurmul = ?, total_simples = ?, total_irrf = ?, total_pis = ?, total_cofins = ?, total_irpj = ?," +
        		" total_csll = ?, total_multas = ?, total_vrprinc = ?, total_jurmul = ?, total_total = ? where debitos.id_valores = ?");
        stmt.setFloat(1, d.getSimples_princ());
        stmt.setFloat(2, d.getSimples_jurmul());
        stmt.setFloat(3, d.getIrrf_princ());
        stmt.setFloat(4, d.getIrrf_jurmul());
        stmt.setFloat(5, d.getPis_princ());
        stmt.setFloat(6, d.getPis_jurmul());
        stmt.setFloat(7, d.getCofins_princ());
        stmt.setFloat(8, d.getCofins_jurmul());
        stmt.setFloat(9, d.getIrpj_princ());
        stmt.setFloat(10, d.getIrpj_jurmul());
        stmt.setFloat(11, d.getCsll_princ());
        stmt.setFloat(12, d.getCsll_jurmul());
        stmt.setFloat(13, d.getMultas_decl_princ());
        stmt.setFloat(14, d.getMultas_decl_jurmul());
        stmt.setFloat(15, d.getTotal_simples());
        stmt.setFloat(16, d.getTotal_irrf());
        stmt.setFloat(17, d.getTotal_pis());
        stmt.setFloat(18, d.getTotal_cofins());
        stmt.setFloat(19, d.getTotal_irpj());
        stmt.setFloat(20, d.getTotal_csll());
        stmt.setFloat(21, d.getTotal_multas());
        stmt.setFloat(22, d.getTotal_vrprinc());
        stmt.setFloat(23, d.getTotal_jurmul());
        stmt.setFloat(24, d.getTotal_total());
        stmt.setInt(25, v.getId());
        stmt.executeUpdate();
    }
    catch (SQLException ex) {
        ex.printStackTrace();
    }
    
    catch (Exception e) {
    	e.printStackTrace();
    }
    finally {
        Banco.closeConexao(conn, rs, stmt);
    }
}

	
}
